﻿using System.Collections.Generic;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using System.IO;

#if WINDOWS

using System.ComponentModel;
using System.Drawing.Design;

#endif
#if WINDOWS || XBOX

using FITClub.Episode1.Components.Particles.ParticleSystems;

#endif

using FITClub.Util;

namespace FITClub.Episode1.Levels
{
    [XmlInclude(typeof(PlatformConfig))]
    [XmlInclude(typeof(FluidDeathConfig))]
    [XmlInclude(typeof(BackgroundItemConfig))]
    [XmlInclude(typeof(PowerUpConfig))]
    public class LevelConfig
    {
        [XmlAttribute]
#if WINDOWS
        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string MusicPath { get; set; }

        [XmlAttribute]
#if WINDOWS
        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string BackgroundPath { get; set; }

        public List<PlatformConfig> Platforms { get; set; }

        public List<BackgroundItemConfig> BackgroundItems { get; set; }

        public List<PowerUpConfig> PowerUpConfigs { get; set; }

#if !WINDOWS_PHONE

        public List<ParticleEffectGenerator> Particles { get; set; }

#endif

        [XmlArrayItem("Point")]
        public List<Vector2> Respawns { get; set; }

        //[XmlArrayItem("Point")]
        //public List<Vector2> PowerUps { get; set; }

#if WINDOWS

        [TypeConverter(typeof(ExpandableObjectConverter))]
#endif

        public FluidDeathConfig Fluid { get; set; }

        public List<Color> HudTextColors { get; set; }

#if WINDOWS

        [TypeConverter(typeof(ExpandableObjectConverter))]
#endif

        public CameraAndPerspective ViewConfig { get; set; }

#if WINDOWS

        [Category("Path")]
        [Editor(typeof(ContentPathEditor), typeof(UITypeEditor))]
        [EditorBrowsable(EditorBrowsableState.Always)]
#endif
        public string WaypointsPath { get; set; }

        public LevelConfig()
        {
            this.HudTextColors = new List<Color>();
        }

        public Level ConstructLevel(GameFITClub game, bool physics)
        {
            Level level = new Level(game, this, physics);
            return level;
        }

        public static Level LoadLevel(string path, GameFITClub game)
        {
            return LoadLevel(path, game, true);
        }

        public static Level LoadLevel(string path, GameFITClub game, bool physics)
        {
#if WINDOWS
            using (Stream fs = File.OpenRead(path))
#else
            using (Stream fs = TitleContainer.OpenStream(path))
#endif
            {
                XmlSerializer xser = new XmlSerializer(typeof(LevelConfig));
                LevelConfig lc = xser.Deserialize(fs) as LevelConfig;
                return lc.ConstructLevel(game, physics);
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        public void SaveLevel(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                XmlSerializer xser = new XmlSerializer(typeof(LevelConfig));
                xser.Serialize(fs, this);
            }
        }
    }
}